<?php

namespace LowCode\Traits;

use BadMethodCallException;
use Closure;

trait HasCall
{
    protected function toCall(string $method, array $parameters)
    {
        $keys = array_flip(self::$allowAttributes);
        if (isset($keys[$method])){
            return $this->setAttribute($method, $parameters[0]);
        }
        if (! static::hasMacro($method)) {
            throw new BadMethodCallException(sprintf(
                'Method %s::%s does not exist.', static::class, $method
            ));
        }

        $macro = static::$macros[$method];

        if ($macro instanceof Closure) {
            $macro = $macro->bindTo($this, static::class);
        }

        return $macro(...$parameters);
    }
}
